package com.taotie.thetoolofgluttony.common;

import java.util.Iterator;

import com.taotie.thetoolofgluttony.SecretKey;

import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.I18n;
import net.minecraft.init.Items;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class EventLoader {
	public EventLoader() {
		MinecraftForge.EVENT_BUS.register(this);
	}

	@SubscribeEvent
	@SideOnly(Side.CLIENT)
	public void onPlayerRightClick(PlayerInteractEvent.RightClickBlock event) {
		if (event.getItemStack().getItem().equals(Items.IRON_SWORD) && !event.getWorld().isRemote) {
			Coordinate.setSecondCoordinate(event.getPos().getX(), event.getPos().getY(), event.getPos().getZ());
			Coordinate.upBoolean(Coordinate.Boolean.secondBoolean);
			event.getEntityPlayer()
					.sendMessage(new TextComponent(String
							.format(I18n.format("text5.inf"), 1, event.getPos().getX(), event.getPos().getY(),
									event.getPos().getZ())
							.substring(13, String.format(I18n.format("text5.inf"), 1, event.getPos().getX(),
									event.getPos().getY(), event.getPos().getZ()).length())));
		}

		if (event.getItemStack().getItem().equals(Items.GOLDEN_SWORD) && !event.getWorld().isRemote) {
			Coordinate.setGenerateCoordinate(event.getPos().getX(), event.getPos().getY(), event.getPos().getZ());
			Coordinate.upBoolean(Coordinate.Boolean.generateBoolean);
			Iterator<BlockPos> blockPos = BlockPos.getAllInBox(Coordinate.firstX, Coordinate.firstY, Coordinate.firstZ,
					Coordinate.secondX, Coordinate.secondY, Coordinate.secondZ).iterator();
			event.getEntityPlayer().sendMessage(new TextComponent(I18n.format("text8.inf")));
			int i = 0;
			while (blockPos.hasNext()) {
				i++;
				blockPos.next();
				if (i >= Integer.MAX_VALUE) {
					event.getEntityPlayer().sendMessage(new TextComponent(I18n.format("text9.inf")));
				}
			}
			event.getEntityPlayer()
					.sendMessage(
							new TextComponent(
									String.format(I18n.format("text6.inf"), event.getPos().getX(),
											event.getPos().getY(), event.getPos().getZ(), i)
											.substring(13,
													String.format(I18n.format("text6.inf"), event.getPos().getX(),
															event.getPos().getY(), event.getPos().getZ(), i)
															.length())));

		}
		if (event.getItemStack().getItem().equals(Items.DIAMOND_SWORD) && !event.getWorld().isRemote) {
			if (SecretKey.isMember(SecretKey.getMd5(ConfigL.secretKey))) {
				Minecraft mc = Minecraft.getMinecraft();
				mc.displayGuiScreen(new GenerateGui(mc.currentScreen));
				Iterator<BlockPos> blockPos = BlockPos.getAllInBox(Coordinate.firstX, Coordinate.firstY,
						Coordinate.firstZ, Coordinate.secondX, Coordinate.secondY, Coordinate.secondZ).iterator();
				while (blockPos.hasNext()) {
					System.out.println(event.getWorld().getBlockState(blockPos.next()).getBlock().getUnlocalizedName());
				}
			} else {
				event.getEntityPlayer()
						.sendMessage(new TextComponent(String.format(I18n.format("error0.inf"), ConfigL.secretKey)
								.substring(13, String.format(I18n.format("error0.inf"), ConfigL.secretKey).length())));
			}
		}
	}

	@SubscribeEvent
	public void onPlayerLeftclick(PlayerInteractEvent.LeftClickBlock event) {
		if (event.getItemStack().getItem().equals(Items.IRON_SWORD) && !event.getWorld().isRemote) {
			Coordinate.setFirstCoordinate(event.getPos().getX(), event.getPos().getY(), event.getPos().getZ());
			Coordinate.upBoolean(Coordinate.Boolean.firstBoolean);
			event.getEntityPlayer()
					.sendMessage(new TextComponent(String
							.format(I18n.format("text5.inf"), 2, event.getPos().getX(), event.getPos().getY(),
									event.getPos().getZ())
							.substring(13, String.format(I18n.format("text5.inf"), 2, event.getPos().getX(),
									event.getPos().getY(), event.getPos().getZ()).length())));
		}
	}

	@SubscribeEvent
	public void onRenderWorldLast(RenderWorldLastEvent event) {
	}
}
